import { createRouter, createWebHistory } from 'vue-router'

// 引入新的组件
const TraditionalPlaysView = () => import('../views/TraditionalPlaysView.vue')
const ModernAdaptationsView = () => import('../views/ModernAdaptationsView.vue')
const PerformanceCalendarView = () => import('../views/PerformanceCalendarView.vue')
const MembershipView = () => import('../views/MembershipView.vue')

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    // 演出详情页面路由
    { path: '/show/:id', name: 'show-details', component: () => import('../views/ShowDetailsView.vue'), meta: { title: '演出详情 - 影韵华彩' } },
    {
      path: '/',
      name: 'welcome',
      component: () => import('../views/WelcomePage.vue'),
      meta: {
        title: '影韵华彩 - 欢迎页面'
      }
    },
    {
      path: '/home',
      name: 'home',
      component: () => import('../views/HomeView.vue'),
      meta: {
        title: '皮影戏 - 首页'
      }
    },
    {
      path: '/about',
      name: 'about',
      component: () => import('../views/AboutView.vue'),
      meta: {
        title: '皮影戏 - 关于皮影'
      }
    },
    {
      path: '/types',
      name: 'types',
      component: () => import('../views/TypesView.vue'),
      meta: {
        title: '皮影戏 - 皮影种类'
      }
    },
    {
      path: '/artists',
      name: 'artists',
      component: () => import('../views/ArtistsView.vue'),
      meta: {
        title: '皮影戏 - 传承艺人'
      }
    },
    {
      path: '/culture',
      name: 'culture',
      component: () => import('../views/CultureView.vue'),
      meta: {
        title: '皮影戏 - 文化传承'
      }
    },
    {
      path: '/contact',
      name: 'contact',
      component: () => import('../views/ContactView.vue'),
      meta: {
        title: '皮影戏 - 联系我们'
      }
    },
    // 经典剧目相关路由
    {
      path: '/traditional-plays',
      name: 'traditional-plays',
      component: TraditionalPlaysView,
      meta: {
        title: '传统剧目 - 影韵华彩'
      }
    },
    {
      path: '/modern-adaptations',
      name: 'modern-adaptations',
      component: ModernAdaptationsView,
      meta: {
        title: '现代改编 - 影韵华彩'
      }
    },
    {
      path: '/performance-calendar',
      name: 'performance-calendar',
      component: PerformanceCalendarView,
      meta: {
        title: '演出日历 - 影韵华彩'
      }
    },
    // 会员中心路由
    {
      path: '/membership',
      name: 'membership',
      component: MembershipView,
      meta: {
        title: '会员中心 - 影韵华彩'
      }
    },
    // 购物车路由
    {
      path: '/cart',
      name: 'cart',
      component: () => import('../views/CartView.vue'),
      meta: {
        title: '购物车 - 影韵华彩'
      }
    },
    // 认证相关路由
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/LoginView.vue'),
      meta: {
        title: '皮影戏 - 登录'
      }
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('../views/RegisterView.vue'),
      meta: {
        title: '皮影戏 - 注册'
      }
    },
    // 404页面
    {
      path: '/:pathMatch(.*)*',
      name: 'not-found',
      component: () => import('../views/NotFoundView.vue'),
      meta: {
        title: '页面未找到'
      }
    }
  ],
  // 滚动行为
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  }
})

// 路由守卫：检查是否已访问过欢迎页面
router.beforeEach((to, from, next) => {
  // 动态设置页面标题
  document.title = to.meta.title || '皮影戏 - 中国非物质文化遗产'
  
  // 检查是否已访问过欢迎页且当前要去欢迎页
  if (to.path === '/' && localStorage.getItem('hasVisitedWelcome') === 'true') {
    // 已经访问过欢迎页，重定向到主页
    next('/home')
  } else {
    // 正常导航
    next()
  }
})

export default router
